/**
 * Copyright (C)  2011  chen wei hua.
 * Permission is granted to copy, distribute and/or modify this document
 * under the terms of the GNU Free Documentation License, Version 2.0
 * or any later version published by the Free Software Foundation;
 * with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.
 * A copy of the license is included in the section entitled "GNU Free Documentation License".
 */
package com.javaeye.chenweihuacwh.web.baseData;

import java.util.ArrayList;
import java.util.List;

import com.javaeye.chenweihuacwh.dto.baseData.Code;
import com.javaeye.chenweihuacwh.service.baseData.ICodeService;
import com.javaeye.chenweihuacwh.util.ListItem;
import com.javaeye.chenweihuacwh.util.ListUtil;
import com.opensymphony.xwork2.ActionSupport;

public class BaseCodeAction extends ActionSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = -1998966732318406119L;

	private ICodeService codeService;
	
	private List<ListItem> codeCategoryList;
	
	private List<Code> baseCodeList;
	
	private Code baseCode;
	
	private String baseCodeId;
	
	private String codeCategory;
	
	public BaseCodeAction() {
		
	}
	
	public String queryBaseCodeJOSN() throws Exception {
		baseCodeList = codeService.getCodesByCategoryId(codeCategory);
		return SUCCESS;
	}
	
	/**
	 *根据分类ID取得常用数据
	 */
	public String queryBaseCodeList() throws Exception {
		codeCategoryList = ListUtil.getCodeCategoryList();
		baseCodeList = codeService.getCodesByCategoryId(codeCategory);
		return SUCCESS;
	}
	
	public String queryForAddBaseCode() throws Exception {
		return SUCCESS;
	}
	
	public String queryBaseCode() throws Exception {
		baseCode = codeService.getCode(baseCodeId);
		return SUCCESS;
	}
	
	public String saveBaseCode() throws Exception {
		baseCode = codeService.saveCode(baseCode);
		codeCategory = baseCode.getCategoryId();
		return queryBaseCodeList();
	}
	
	public String deleteBaseCode() throws Exception {
		baseCode = codeService.getCode(baseCodeId);
		codeCategory = baseCode.getCategoryId();
		boolean success = codeService.removeCode(baseCodeId);
		queryBaseCodeList();
		if (!success) {
			List<Object> params = new ArrayList<Object>();
			params.add("常用数据");
			this.addActionError(getText("actionErrorDeleteFK", params));
			return INPUT;
		}
		return SUCCESS;
	}

	public List<ListItem> getCodeCategoryList() {
		return codeCategoryList;
	}

	public void setCodeCategoryList(List<ListItem> codeCategoryList) {
		this.codeCategoryList = codeCategoryList;
	}

	public List<Code> getBaseCodeList() {
		return baseCodeList;
	}

	public void setBaseCodeList(List<Code> baseCodeList) {
		this.baseCodeList = baseCodeList;
	}

	public Code getBaseCode() {
		return baseCode;
	}

	public void setBaseCode(Code baseCode) {
		this.baseCode = baseCode;
	}

	public String getBaseCodeId() {
		return baseCodeId;
	}

	public void setBaseCodeId(String baseCodeId) {
		this.baseCodeId = baseCodeId;
	}

	public String getCodeCategory() {
		return codeCategory;
	}

	public void setCodeCategory(String codeCategory) {
		this.codeCategory = codeCategory;
	}

	public void setCodeService(ICodeService codeService) {
		this.codeService = codeService;
	}
	
}
